package com.atguigu.gmall.realtime.app;

import com.atguigu.gmall.realtime.util.FlinkSourceUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author lzc
 * @Date 2022/12/26 15:03
 */
public abstract class BaseApp {
    public abstract void handle(StreamExecutionEnvironment env,
                                DataStreamSource<String> stream);
    
    public void init(int port,
                     int p,
                     String ckAndGroupIdAndJobName,
                     String topic){
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        // 1. 创建流的执行环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);  // 如果不设置, 则会使用一个随机的 web 端口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p); // 一般和 kafka 的分区数保持一致
        // 设置状态后端和 checkpoint
        env.setStateBackend(new HashMapStateBackend());// 1. HashMap状态后端  2. rocksdb 状态后端
        env.enableCheckpointing(3000); // 设置 checkpoint 的周期: 生成环境一般是分钟级别
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1); // 设置同时进行的 checkpoint 的数量
        // 配置这个, 上面这个可以不设置
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);// 上一个 checkpoint 结束 500ms 之后,才会开启下一个
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);  // checkpoint 的超时时间
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall/"+ckAndGroupIdAndJobName);
        // 2. 从 kafka 消费数据: 两种方式
        // 1. 旧的方式: SourceFunction 从 1.14 标记为过时, 到 1.17 删除  addSource
        // 2. 新的方式: Kafka Source   fromSource
        DataStreamSource<String> stream = env.fromSource(FlinkSourceUtil.getKafkaSource(ckAndGroupIdAndJobName, topic),
                                                         WatermarkStrategy.noWatermarks(),
                                                         "kafka-source");
        // 每个子类拿到流之后, 处理的逻辑的不一样
        handle(env, stream);
    
    
    
        // 3. 过滤出需要的数据
    
        // 4. 把数据写入到 Phoenix 中
    
    
        try {
            env.execute(ckAndGroupIdAndJobName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
